import { createApp } from 'vue'
// import { Tabbar, TabbarItem, Checkbox, Button, Searchbar } from '@nutui/nutui-taro'
// import { Swiper, SwiperItem } from '@nutui/nutui-taro'
import { IconFont } from '@nutui/icons-vue-taro'
import './app.scss'
import './assets/font/iconfont.css'

import { ref, onMounted, computed } from "vue";
import Taro from "@tarojs/taro";

import http from "@/utils/http";
import { buildUrlParams } from "@/utils/tooler";
import { globalState } from "@/store/globalStore";
import { store } from "@/store/index";
import { ShareUtils } from "@/utils/share";
const params = Taro.getCurrentInstance().router?.params;

const App = createApp({
  onShow(options) {
  },
  async onLaunch() {
    // console.log("onLaunch");
    const loginRes = await Taro.login();
    console.log("loginRes", loginRes);
    // // return;

    if (loginRes.code) {
      // console.log("code:", res);
      http
      .post(
        buildUrlParams("/api/weixin/getToken", {
          code: loginRes.code,
          nickName: "微信用户",
          avatarUrl: "",
          referralCode: params?.code || "",
        }),
        null,
        { headers: { token: "1" } }
      )
      .then((res) => {
        console.log("res", res);
        // return;
        if (res.code === 0) {
          // isBusiness.value = res.data.userType == "B";
          globalState.setToken(res.data.token);
          // globalState.getUserInfo();
          store.setToken(res.data.token);
          store.setUserType(res.data.userType);
          store.getUserInfo();
        }
      });
    }
  },
  // 全局分享到好友配置
  onShareAppMessage(res) {
    Taro.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    console.log('全局分享到好友', res);
    // return ShareUtils.generateShareAppMessage();
    return {
      title: "森链智选",
      path: "/pages/home/index", // 路由路径
      imageUrl: "https://docs.taro.zone/img/logo-taro.png",
      success(res) {
        console.log('分享成功')
      },
      fail(res) {
        console.log('分享失败')
      }
    }
  },
  
  // 全局分享到朋友圈配置 (仅在微信小程序中支持)
  onShareTimeline() {
    console.log('全局分享到朋友圈');
    return ShareUtils.generateShareTimeline();
  },
  
  // 入口组件不需要实现 render 方法，即使实现了也会被 taro 所覆盖
})


// App.use(Tabbar)
// App.use(TabbarItem)
// App.use(Checkbox)
// App.use(Button)
// App.use(Searchbar)
App.use(IconFont as any);
// App.use(pinia);

export default App
